package com.blacksheep.mtm.service.Impl;

import com.blacksheep.mtm.common.exception.XmallException;
import com.blacksheep.mtm.dao.TbHospitalMapper;
import com.blacksheep.mtm.pojo.common.DataTablesResult;
import com.blacksheep.mtm.pojo.manager.TbHospital;
import com.blacksheep.mtm.pojo.manager.TbHospitalExample;
import com.blacksheep.mtm.service.HospitalService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HospitalServiceImpl implements HospitalService{
    
    @Autowired
    private TbHospitalMapper hospitalMapper;
    
    @Override
    public DataTablesResult getHospitalList(int draw, int start, int length, int[] state, String search, String minDate, String maxDate, String orderCol, String orderDir) {

        DataTablesResult result=new DataTablesResult();

        PageHelper.startPage(start/length+1,length);
        List<TbHospital> list = hospitalMapper.selectList("%"+search+"%",state,minDate,maxDate,orderCol,orderDir);
        PageInfo<TbHospital> pageInfo=new PageInfo<>(list);
        result.setRecordsFiltered((int)pageInfo.getTotal());
        result.setDraw(draw);
        result.setData(list);

        return result;
        
    }

    @Override
    public TbHospital insert(TbHospital hospital) {
        int result = hospitalMapper.insertSelective(hospital);
        if (result<1){
            throw new XmallException("新增失败");
        }
        return hospital;
    }

    @Override
    public int deletBatch(Integer[] ids) {
        int result = hospitalMapper.deleteBatchById(ids);
        if(result!=ids.length){
            throw new XmallException("删除失败");
        }
        return 0;
    }

    @Override
    public TbHospital updateHospital(TbHospital hospital) {
        int result = hospitalMapper.updateByPrimaryKeySelective(hospital);
        if (result<1){
            throw new XmallException("更新失败");
        }
        return null;
    }

    @Override
    public TbHospital alertState(Integer id, Integer state) {
        int result = hospitalMapper.updateStateByPrimaryKey(id,state);
        if (result<1){
            throw new XmallException("更新失败");
        }
        return null;
    }

    @Override
    public List<TbHospital> selectListFront() {
        TbHospitalExample example = new TbHospitalExample();
        example.createCriteria().andStateNotEqualTo(-1);
        List<TbHospital> list = hospitalMapper.selectByExample(example);
        return list;
    }
}
